<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>活动管理</cite></a>
    <a layadmin-event="back">返回列表</a>
    <script type="text/html" template>
      <a><cite>添加盲盒空投</cite></a>
    </script>
  </div>
</div>

<script type="text/html" template
        lay-url="{{layui.router().search.id ? 'admin/xxxxs/' + layui.router().search.id : ''}}">
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-header">
        添加空投
      </div>
      <div class="layui-card-body">
        <div style="display: inline-block;">
          <button class="layui-btn layui-btn-info" onclick="add_tr();return false;">添加一行</button>
        </div>
        <div style="position:relative;display: inline-block;width:62px;height:auto;margin-left:5px;">
          <button id="upload" onclick="return false;" class="layui-btn">导入</button>
        </div>
        <div class="layui-inline layui-word-aux" style="padding-top: 20px">
          <a href="/assets/excel/模板.xls" style="text-decoration:underline;" class="layui-font-blue">下载模板</a>
        </div>
        <div class="layui-form">
          <table class="layui-table">
            <thead>
            <tr>
              <td>手机号</td>
              <td>商品id</td>
              <td>数量</td>
              <td>操作</td>
            </tr>
            </thead>
            <tbody id="tbody">
            </tbody>
          </table>

          <div class="layui-form-item">
            <div class="layui-footer">
              <div class="layui-input-block">
                <input type="button" lay-submit lay-filter="submit_airdrops" value="保存" class="layui-btn">
                <input type="button" layadmin-event="back" value="返回" class="layui-btn layui-btn-primary">
              </div>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
  <div style="margin-bottom: 60px"></div>
</script>
<script src="/assets/js/jquery.js"></script>
<script>


  function remove_tr(obj) {
    $(obj).parents('tr').remove();
    return false;
  }

  function add_tr() {
    var tr = create_tr();
    $('#tbody').append(tr);
    return false;
  }

  function create_tr(obj = null) {
    if (obj) {
      return "<tr>" +
        "<td><input type='text'  class=\"layui-input\" name='mobile[]' value='" + obj[0] + "'></td>" +
        "<td><input type='text'  class=\"layui-input\" name='goods_id[]' value='" + obj[1] + "'></td>" +
        "<td><input type='text'  class=\"layui-input\" name='number[]' value='" + obj[2] + "'></td>" +
        "<td><button class='layui-btn layui-btn-sm layui-btn-danger' onclick='remove_tr(this);return false;'>删除</button></td>" +
        "</tr>";
    }
    return "<tr>" +
      "<td><input type='text' class=\"layui-input\" name='mobile[]'></td>" +
      "<td><input type='text' class=\"layui-input\" name='goods_id[]'></td>" +
      "<td><input type='text' class=\"layui-input\" name='number[]'></td>" +
      "<td><button class='layui-btn layui-btn-sm layui-btn-danger' onclick='remove_tr(this);return false;'>删除</button></td>" +
      "</tr>";
  }


  layui.use([], function () {
    let $ = layui.$
      , form = layui.form
      , upload = layui.upload
      , admin = layui.admin;

    let resourceUrl = 'agent-admin/airdrops/blind_box/store';
    //提交
    var sub_flag = true;
    form.on('submit(submit_airdrops)', function (d) {
      if (!sub_flag) {
        return false;
      }
      if (Object.keys(d.field).length == 0) {
        layer.alert('请添加空投数据');
        return false;
      }
      let field = d.field;
      layer.load();
      admin.post(resourceUrl, field, function (res) {
        layer.msg('操作成功', {
          offset: '15px'
          , icon: 1
          , time: 1000
        }, function () {
          location.hash = '/airdrop/blind_box';
        });

        sub_flag = true;
      }, {hasLoading: true})
      sub_flag = true;
      return false;
    })

    var uploadInst = upload.render({
      elem: '#upload' //绑定元素
      , url: 'agent-admin/airdrops/export' //上传接口
      , exts: 'xls'
      , done: function (res) {
        if (res.code !== 0) {
          layer.msg(res.msg);
          return false;
        }
        var data = res.data;

        for (var key in data) {
          // if (key !== '1'){
          if (!!data[key][0] && !!data[key][1]) {
            var tr = create_tr(data[key]);
            $('#tbody').append(tr);
          }

          // }

        }
        return false;
      }
      , error: function () {
        //请求异常回调
      }
    });
  })

</script>
